package com.lv.controller;


import com.lv.data.Result;
import com.lv.service.ContentService;
import com.lv.vo.ContentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("content")
public class ContentController {

    @Autowired
    private ContentService contentService;

    @PostMapping
    public Result getAll(){
        List<ContentVO> contentVOList = contentService.getAll();
        return Result.OK().setData(contentVOList);
    }

    @PostMapping("addContent")
    public Result addContent(@RequestBody ContentVO vo){
        contentService.addContent(vo);
        return Result.OK();
    }

    @PostMapping("toUpdate")
    public Result toUpdate(@RequestParam Long id){
        ContentVO vo = contentService.toUpdate(id);
        return Result.OK().setData(vo);
    }

    @PostMapping("updateContent")
    public Result updateContent(@RequestBody ContentVO vo){
        contentService.updateContent(vo);
        return Result.OK();
    }

    //删除
    @DeleteMapping
    public Result deleteBatch(@RequestParam Long[] ids){
        try {
            contentService.deleteBatch(ids);
            return Result.OK();
        }catch (Exception e){
            e.printStackTrace();
            return Result.ER();
        }
    }

    @PostMapping("getContentByCategoryId")
    public Result getContentByCategoryId(@RequestParam Integer categoryId, @RequestParam(defaultValue = "4")Integer pageSize){
        try {
            List<ContentVO> contentList = contentService.getContentByCategoryId(categoryId,pageSize);
            return Result.OK().setData(contentList);
        }catch (Exception e){
            return Result.ER();
        }
    }
}
